package com.guchenbo.example.dubbo.rpc.protocol;

import com.guchenbo.example.dubbo.template.ProtocolTemplate;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.protocol.AbstractExporter;

/**
 * 使用Map来关联消费者和提供者
 */
public class GcbProtocol extends ProtocolTemplate {

  @Override
  public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
    GcbExporter exporter = new GcbExporter(invoker);
    super.exporterMap.computeIfAbsent(invoker.getUrl().getServiceKey(), (k) -> exporter);
    return exporter;
  }

  @Override
  public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
    return new GcbInvoker(type, url, super.exporterMap.get(url.getServiceKey()).getInvoker());
  }

  class GcbExporter<T> extends AbstractExporter<T> {

    public GcbExporter(Invoker<T> invoker) {
      super(invoker);
    }
  }
}
